  var daniel_physics;
  var daniel_lastFrame = new Date().getTime();
  
  //initialization,
  var daniel_timer = 10000; 
  var daniel_eggs = new Array();
  var daniel_winlose ="";
  var daniel_level = 1;
  
  var img_egg = new Image();
  var img_electrifiedegg = new Image();
  var img_plug = new Image();
  
//game loop
window.daniel_gameLoop = function() {
    var tm = new Date().getTime();
    requestAnimationFrame(daniel_gameLoop);
    var dt = (tm - daniel_lastFrame) / 1000;
    if(dt > 1/15) { dt = 1/15; }

	var params = new Array();
    params['timer']=daniel_timer;
    params["winlose"]=daniel_winlose;
    params["level"]=daniel_level;
    daniel_physics.step(dt,params);
    daniel_lastFrame = tm;
 
	if (daniel_timer==0)	{
		daniel_timer =-1;
		daniel_winlose = "YOU LOSE";
    }
    else {
    	var wincheck=true;
    	for (index = 0; index < daniel_eggs.length; ++index) {
    		if (daniel_eggs[index].details.image != img_electrifiedegg){
    	    	wincheck=false;
    	    }
    	}
    	if (daniel_timer!= -1 && wincheck==true)	{
    		daniel_level++;
    		if (daniel_level>10){
    			daniel_timer=-1;
    			daniel_winlose = "YOU WIN";
    		}
    		else	{
    			daniel_timer=2000+(daniel_level*1000);	
    			daniel_init();

    		}
    	}
    	if (daniel_timer>0)	{daniel_timer --;}
    }
};

function daniel_init() {
    img_egg.src = "img/theme4/collectible1.png";
    img_electrifiedegg.src = "img/theme4/collectible2.png";
    img_plug.src = "img/theme4/collectible3.png";
    
	  // Wait for the image to load
	 // img.addEventListener("load", function() {

	  daniel_physics = window.physics = new daniel_Physics(document.getElementById("canvas"));
		
	  /*
	  document.getElementById('selectlevel').onclick = function() {
		  daniel_level=prompt("Please enter desired level (1-10)","1");
		  daniel_init();
		  daniel_timer=10000;
	  };
	  */
		
	  daniel_physics.collision();
      // Create some walls
      new daniel_Body(daniel_physics, { color: "blue", type: "static", x: 0, y: 0, height: 50,  width: 0.5 });
      new daniel_Body(daniel_physics, { color: "blue", type: "static", x:40, y: 0, height: 50,  width: 0.5});
      new daniel_Body(daniel_physics, { color: "blue", type: "static", x: 0, y: 0, height: 0.5, width: 120 });
      new daniel_Body(daniel_physics, { color: "blue", type: "static", x: 0, y:22.5, height: 0.5, width: 120 });

      daniel_eggs = new Array();

      if (daniel_level==1){
    	  daniel_eggs[0] = new daniel_Body(daniel_physics, {image:img_egg, type: "static", shape: "circle", radius:2, x: 30, y: 8, width:4, height:4});
          
    	  new daniel_Body(daniel_physics, { image:img_plug, type: "static", x: 30, y:22, height: 0.5, width: 10 });
          
          new daniel_Body(daniel_physics, { color: "green", x: 1, y: 8 });
          new daniel_Body(daniel_physics, { color: "green", x: 5, y: 8 });
          new daniel_Body(daniel_physics, { color: "green", x: 5, y: 8 });
      }
      else if (daniel_level ==2){
    	  daniel_eggs[0] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 15, y: 7.5, width:4, height:4 });
    	  new daniel_Body(daniel_physics, { image:img_plug, type: "static", x: 30, y:22, height: 0.5, width: 10 });
          
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 20, width: 10, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", shape: "polygon", 
              points: [ { x: 0, y: 0 }, { x: 0, y: 4 },{ x: -10, y: 5 }, {x: -12, y: 3}   ],
              x: 20, y: 5 });
      }
      else if (daniel_level ==3){
    	  //create the eggs
    	  daniel_eggs[0] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 10, y: 17, width:4, height:4 });
    	  daniel_eggs[1] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 20, y: 17, width:4, height:4 });
          
          //create the plug - electrical source
          new daniel_Body(daniel_physics, { image:img_plug, type: "static", x: 30, y:22, height: 0.5, width: 10 });

          //create the blocks
          new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 15, height: 0.5 });
          new daniel_Body(daniel_physics, { color: "green", x: 13, y: 8 });
          new daniel_Body(daniel_physics, { color: "green", shape: "circle", radius: 2, x: 5, y: 20 });
      }
      else if (daniel_level==4){
    	  //create the eggs
    	  daniel_eggs[0] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 3, x: 10, y: 10, width:4, height:4 });
    	  daniel_eggs[1] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 3, x: 25, y: 10, width:4, height:4 });
          
          //create the plug - electrical source
          new daniel_Body(daniel_physics, { image:img_plug, type: "static", x: 15, y:22, height: 0.5, width: 10 });

          //create the blocks
          new daniel_Body(daniel_physics, { color: "green", x: 13, y: 8 });
          new daniel_Body(daniel_physics, { color: "green", shape: "polygon", 
                              points: [ { x: 0, y: 0 }, { x: 0, y: 4 },{ x: -10, y: 0 }   ],
                              x: 20, y: 5 });
          new daniel_Body(daniel_physics, { color: "green", shape: "polygon", 
              points: [ { x: 0, y: 0 }, { x: 0, y: 4 },{ x: -10, y: 0 }   ],
              x: 20, y: 5 });

      }
      else if (daniel_level==5){
    	  daniel_eggs[0] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2.5, x: 40, y: 5, width:4, height:4 });
    	  new daniel_Body(daniel_physics, { image:img_plug, type: "static", x: 5, y:22, height: 0.5, width: 10 });

    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 30, height: 0.5 });
  		  new daniel_Body(daniel_physics, { color: "green", shape: "polygon", 
  			  points: [ { x: 0, y: 0 }, { x: 0, y: 4 },{ x: -10, y: 0 }   ],
  			  x: 20, y: 5 });     
      }
      else if (daniel_level==6){
    	  daniel_eggs[0] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2.5, x: 35, y: 10 , width:4, height:4});
    	  daniel_eggs[1] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 30, y: 15, width:4, height:4 });
    	  
    	  new daniel_Body(daniel_physics, { image:img_plug, type: "static", x: 0.5, y:22, height: 0.5, width: 10 });
    	  
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 15, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 30, height: 0.5 });
      }
      else if (daniel_level==7){
    	  daniel_eggs[0] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 35, y: 5 , width:4, height:4});
    	  daniel_eggs[1] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 35, y: 15, width:4, height:4 });
    	  
    	  new daniel_Body(daniel_physics, { image:img_plug, type: "static", x: 20, y:22, height: 0.5, width: 10 });
     	 
    	  new daniel_Body(daniel_physics, { color: "blue", type: "static", x: 40, y:10, height: 0.5, width: 25 });
    	  
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 10, height: 0.5 });    	  
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 10, height: 0.5 });    	  
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 10, height: 0.5 });    	  
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 20, height: 0.5 });
      }
      else if (daniel_level==8){
    	  daniel_eggs[0] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 35, y: 5, width:4, height:4 });
    	  daniel_eggs[1] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 35, y: 15, width:4, height:4 });
    	  daniel_eggs[2] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 25, y: 10, width:4, height:4 });
    	  
    	  new daniel_Body(daniel_physics, { image:img_plug, type: "static", x: 10, y:0.5, height: 0.5, width: 10 });
      	 
    	  new daniel_Body(daniel_physics, { color: "blue", type: "static", x: 15, y:25, height: 20, width: 5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 10, height: 0.5 });    	  
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 10, height: 0.5 });    	  
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 10, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 15, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 4, width: 20, height: 0.5 });
    	  
      }
      else if (daniel_level==9){
    	  daniel_eggs[0] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 35, y: 5, width:4, height:4 });
    	  daniel_eggs[1] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 35, y: 15, width:4, height:4 });
    	  
    	  new daniel_Body(daniel_physics, { image:img_plug, type: "static", x: 0.5, y:22, height: 0.5, width: 10 });
      	 
    	  new daniel_Body(daniel_physics, { color: "blue", type: "static", x: 30, y:10, height: 0.5, width: 30 });
    	  new daniel_Body(daniel_physics, { color: "blue", type: "static", x: 40, y:10, height: 10, width: 0.5 });
     	
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 15, width: 10, height: 0.5 });    	  
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 15, width: 15, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 15, width: 15, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 15, width: 10, height: 0.5 });    	  
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 15, width: 20, height: 0.5 });    	  
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 20, width: 10, height: 0.5 });
    	
      }
      else if (daniel_level==10){
    	  daniel_eggs[0] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 25, y: 5, width:4, height:4 });
    	  daniel_eggs[1] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 30, y: 10, width:4, height:4 });
    	  daniel_eggs[2] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 25, y: 15, width:4, height:4 });
    	  daniel_eggs[3] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 20, y: 10, width:4, height:4 });
    	  daniel_eggs[4] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 20, y: 2.5, width:4, height:4 });
    	  daniel_eggs[5] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 30, y: 2.5, width:4, height:4 });
    	  daniel_eggs[6] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 20, y: 17.5, width:4, height:4 });
    	  daniel_eggs[7] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 30, y: 17.5, width:4, height:4 });
    	  daniel_eggs[8] = new daniel_Body(daniel_physics, { image:img_egg, type: "static", shape: "circle", radius: 2, x: 25, y: 10, width:4, height:4 });
    	  
    	  new daniel_Body(daniel_physics, { image:img_plug, type: "static", x: 5, y:10, height: 0.5, width: 10 });
      	 
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 20, width: 15, height: 0.5 });
    	  
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 20, width: 5, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 20, width: 5, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 20, width: 5, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 20, width: 5, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 20, width: 5, height: 0.5 });

    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 20, width: 5, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 20, width: 5, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 20, width: 5, height: 0.5 });
    	  new daniel_Body(daniel_physics, { color: "green", x: 4, y: 20, width: 5, height: 0.5 });

      }
      
      daniel_physics.dragNDrop();
      requestAnimationFrame(daniel_gameLoop);
    //});
  
}
	  
//  window.addEventListener("load",daniel_init);
//}());

//Lastly, add in the `requestAnimationFrame` shim, if necessary. Does nothing 
//if `requestAnimationFrame` is already on the `window` object.
(function() {
 var lastTime = 0;
 var vendors = ['ms', 'moz', 'webkit', 'o'];
 for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
     window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
     window.cancelAnimationFrame = 
       window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
 }

 if (!window.requestAnimationFrame) {
     window.requestAnimationFrame = function(callback, element) {
         var currTime = new Date().getTime();
         var timeToCall = Math.max(0, 16 - (currTime - lastTime));
         var id = window.setTimeout(function() { callback(currTime + timeToCall); }, 
           timeToCall);
         lastTime = currTime + timeToCall;
         return id;
     };
 }

 if (!window.cancelAnimationFrame) {
     window.cancelAnimationFrame = function(id) {
         clearTimeout(id);
     };
 }
}());
